package com.wc.alorithm_blue_bridge._LCA.LCA树上倍增;

/**
 * @Author congge
 * @Date 2024/5/30 1:01
 * @description
 * https://www.lanqiao.cn/problems/8617/learning/?page=1&first_category_id=1
 *
 */
import java.util.Scanner;
import java.util.Arrays;
import java.io.PrintWriter;

public class Main {
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010, M = N << 1, INF = 0x3f3f3f3f, idx = 1;
    static int[] h = new int[N], e = new int[M], ne = new int[M], depth = new int[N], q = new int[N];
    static int[][] f = new int[N][18];
    static int n, m;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        for(int i = 1; i < n; i++){
            int a = sc.nextInt(), b = sc.nextInt();
            add(a, b);
            add(b, a);
        }
        bfs();
        m = sc.nextInt();
        while(m-- > 0){
            int a = sc.nextInt(), b = sc.nextInt();
            int res = lca(a, b);
            out.println(res);
        }
        out.flush();
    }

    static int lca(int a, int b){
        if(depth[a] < depth[b]){
            return lca(b, a);
        }
        for(int i = 17; i >= 0; i--){
            if(depth[f[a][i]] >= depth[b]){
                a = f[a][i];
            }
        }
        if (a == b) return a;
        for(int i = 17; i >= 0; i--){
            if(f[a][i] != f[b][i]){
                a = f[a][i];
                b = f[b][i];
            }
        }

        return f[a][0];
    }

    static void add(int a, int b){
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    static void bfs(){
        Arrays.fill(depth, INF);
        depth[0] = 0;
        depth[1] = 1;
        int hh = 1, tt = 0;
        q[++tt] = 1;
        while(hh <= tt){
            int t = q[hh ++];
            for(int i = h[t]; i > 0; i = ne[i]){
                int j = e[i];
                if(depth[j] > depth[t] + 1){
                    depth[j] = depth[t] + 1;
                    q[++tt] = j;
                    f[j][0] = t;
                    for(int k = 1; k <= 17; k++){
                        f[j][k] = f[f[j][k - 1]][k - 1];
                    }
                }
            }
        }
    }
}
